import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';

class CountDownTextButton extends StatefulWidget {
  final String text;
  final Function onPressed;
  final int count;
  final Color? textColor;
  final Color? backgroundColor;
  final Color? borderColor;
  final double? width;
  final double? height;
  final double? fontSize;
  final double? borderWidth;
  final EdgeInsets? padding;
  final EdgeInsets? margin;
  final TextStyle? textStyle;

  const CountDownTextButton({
    Key? key,
    required this.text,
    required this.onPressed,
    required this.count,
    this.textColor,
    this.backgroundColor,
    this.borderColor,
    this.width,
    this.height,
    this.fontSize,
    this.borderWidth,
    this.padding,
    this.margin,
    this.textStyle,
  });

  @override
  State<StatefulWidget> createState() => _CountDownTextButton();
}

class _CountDownTextButton extends State<CountDownTextButton> {
  TimerUtil? _timerUtil;

  double num = 0.0;

  _countDown() {
    //计时60秒
    int totalTime = widget.count * 1000;
    // 初始化设置
    _timerUtil = new TimerUtil(mTotalTime: totalTime);
    _timerUtil?.setOnTimerTickCallback((int tick) {
      setState(() {
        num = tick / 1000;
      });
    });
    _timerUtil?.startCountDown();
  }

  @override
  void initState() {
    super.initState();
    _countDown();
  }

  @override
  void dispose() {
    super.dispose();
    if (_timerUtil != null && _timerUtil!.isActive()) {
      _timerUtil?.cancel();
    }
  }

  @override
  Widget build(BuildContext context) {
    return IgnorePointer(
        ignoring: num != 0.0,
        child: InkWell(
            onTap: () {
              widget.onPressed.call();
            },
            child: Container(
                width: widget.width ?? 130,
                alignment: Alignment.center,
                margin: widget.margin,
                padding: widget.padding ?? EdgeInsets.symmetric(horizontal: 5),
                height: widget.height ?? 35,
                decoration: BoxDecoration(
                  color: widget.backgroundColor,
                  border: Border.all(
                    color: widget.borderColor ?? Colors.grey, // 边框颜色
                    width: 0.5, // 边框宽度
                  ),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      widget.text,
                      style: TextStyle(
                          color: num == 0.0
                              ? (widget.textColor ?? Colors.black)
                              : Colors.grey,
                          fontSize: widget.fontSize ?? 15),
                    ),
                    num != 0.0
                        ? Text(
                            '(' + num.toInt().toString() + ')',
                            style: TextStyle(color: Colors.grey),
                          )
                        : Container()
                  ],
                ))));
  }
}
